package com.hbnu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbnu.mapper.WordMapper;
import com.hbnu.pojo.Word;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class WordService {
    @Autowired
    private WordMapper wordMapper;

    private int count;
    //随机在1-20中生成几个不同的数字（用于查询绘画词）
    public List<Integer> getRandomNumbers(int count) {
        if (count < 0 || count > 20) {
            throw new IllegalArgumentException("数量必须在0到20之间");
        }

        List<Integer> numbers = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            numbers.add(i);
        }

        Collections.shuffle(numbers); // 打乱顺序
        return numbers.subList(0, count); // 取前count个
    }
    //获取生成数
    List<Integer> numbers = getRandomNumbers(count);

    //根据生成的随机数组获取单词组
    public List<Word> getWordsByNumbers(List<Integer> numbers) {
        //获取单词组
        List<Word> words = new ArrayList<>();
        for (Integer number : numbers){
            Word word = wordMapper.selectOne(new QueryWrapper<Word>().eq("word_id", number));
            if(word != null){
                words.add(word);
            }
        }
        return words;
    }

    //获得生成的单词组
    List<Word> words = getWordsByNumbers(numbers);;


    //根据单词组来获得单词文本
    public List<String> getWordTextByWords(List<Word> words){
        List<String> wordTexts = new ArrayList<>();
        for (Word word : words){
            wordTexts.add(word.getWordText());
        }
        return wordTexts;
    }
    //根据单词组来获得单词字数
    public List<Integer> getWordLengthByWords(List<Word> words){
        List<Integer> wordLengths = new ArrayList<>();
        for (Word word : words){
            wordLengths.add(word.getNumber());
        }
        return wordLengths;
    }

    //根据单词组来获得单词类型
    public List<String> getWordCategoryByWords(List<Word> words){
        List<String> wordCategories = new ArrayList<>();
        for (Word word : words){
            wordCategories.add(word.getCategory());
        }
        return wordCategories;
    }

    //随机获取一个词汇
    public Word getRandomWord() {
        List<Integer> randomNumbers = getRandomNumbers(1);
        if (!randomNumbers.isEmpty()) {
            return wordMapper.selectOne(new QueryWrapper<Word>().eq("word_id", randomNumbers.get(0)));
        }
        return null;
    }

}
